#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.system.ApplicationHome;

import java.io.File;

/**
 * Description: 获取项目的位置
 *
 * @author wupanhua
 * @date 2019/8/6 15:28
 *
 * <pre>
 *              ${copyright}
 *      Copyright (c) 2019. All Rights Reserved.
 * </pre>
 */
@Slf4j
public class ProjectHomePathUtil {

    private static final String JAR_FILE_SUFFIX = ".jar!";

    private ProjectHomePathUtil(){
        // make constructor private
    }
    /**
     * Description:
     * <获取项目的根路径 jar包获得的是同级目录>
     * @author wupanhua
     * @date 15:40 2019/8/8
     * @return java.lang.String
     **/
    public static String getHomePath() {

        // 检测当前项目的部署方式
        String path = ProjectHomePathUtil.class.getResource("/").getPath();
        if(!path.endsWith(File.separator)) {
            path += File.separator;
        }
        // jar 包部署的形式
        if (path.indexOf(JAR_FILE_SUFFIX) != -1) {
            ApplicationHome home = new ApplicationHome(ProjectHomePathUtil.class);
            return home.getSource().getParentFile().toString();
        }

        return path;
    }

    /**
     * Description:
     * <获得项目根路径的File对象>
     * @author wupanhua
     * @date 15:41 2019/8/8
     * @return java.io.File
     **/
    public static File getHomePathAsFile() {

        String path = getHomePath();
        return new File(path);
    }

    /**
     * Description:
     * <获取传入的相对路径在项目中的位置>
     * @author wupanhua
     * @date 15:41 2019/8/8
     * @param rpath 1
     * @return java.lang.String
     **/
    public static String getHomePath(String rpath) {

        String path = getHomePath();
        if (rpath == null) {
            return path;
        }

        return new StringBuilder(path).append(rpath).toString();
    }

    /**
     * Description:
     * <获取传入的相对路径在项目中的位置>
     * @author wupanhua
     * @date 15:41 2019/8/8
     * @param rpath 1
     * @return java.io.File
     **/
    public static File getHomePathAsFile(String rpath) {

        String path = getHomePath(rpath);

        if (rpath == null) {
            return getHomePathAsFile();
        }

        return new File(path);
    }

}
